package com.sxw.hw_student.system.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxw.hw_student.system.entity.SysRole;
import com.sxw.hw_student.system.service.ISysRoleService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.awt.dnd.Autoscroll;
import java.time.chrono.IsoChronology;



/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sxw
 * @since 2025-04-02
 */
@RestController
@RequestMapping("/system/sysRole")
@Api(tags = "角色模块")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @PostMapping("/add")
    public ResponseEntity<String> addRoleWithAdmin(@RequestBody SysRole sysRole) {
        sysRoleService.testRoleAdd(sysRole);
        return ResponseEntity.ok("添加成功");
    }

    @PostMapping("/save")
    public ResponseEntity<String> saveRole(@RequestBody SysRole sysRole) {
        sysRoleService.save(sysRole);
        return ResponseEntity.ok("保存成功");
    }

    @PutMapping("/update")
    public ResponseEntity<String> updateRole(@RequestBody SysRole sysRole) {
        return sysRoleService.updateById(sysRole)
                ? ResponseEntity.ok("更新成功")
                : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("更新失败");
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> deleteRole(@PathVariable Long id) {
        return sysRoleService.removeById(id)
                ? ResponseEntity.ok("删除成功")
                : ResponseEntity.status(HttpStatus.NOT_FOUND).body("删除失败，角色不存在");
    }

    @GetMapping("/{id}")
    public ResponseEntity<SysRole> getRoleById(@PathVariable Long id) {
        SysRole role = sysRoleService.getById(id);
        return role != null ? ResponseEntity.ok(role) : ResponseEntity.notFound().build();
    }

    @GetMapping("/page")
    public Page<SysRole> pageRole(@RequestParam(required = false) String roleName,
                                  @RequestParam(defaultValue = "1") Integer current,
                                  @RequestParam(defaultValue = "10") Integer size) {
        Page<SysRole> page = new Page<>(current, size);
        return sysRoleService.queryRolePage(page, roleName);
    }
}







